package org.fastiot

import grails.testing.gorm.DomainUnitTest
import spock.lang.Specification

class TerminalSpec extends Specification implements DomainUnitTest<Terminal> {

    def setup() {
    }

    def cleanup() {
    }

    void "Test a valid properties"() {
        given:
        def instance = new Terminal(
                code: code, authCode: authCode, model: model, group: group, customizations: customizations
        )

        when:
        instance.save(flush: true)

        then:
        !instance.hasErrors()

        where:
        code << ["LN1234567890424567", "LN1234567890424567"]
        authCode << ["80283234023420234362", "80283234023420234362"]
        model << [null, new Model(code: "m1", name: "型号1")]
        group << [null, new Group(code: "g1", name: "分组1")]
        customizations << [null, [hardwareVer: "334554321123322", softwareVer: "334554321123211"]]
    }

    void "Test an invalid properties"() {
        given:
        def instance = new Terminal(
                code: code, authCode: authCode, modelCode: "m1", groupName: "分组1", management: management,
                lastAccessAddress: lastAccessAddress
        )

        when:
        instance.save(flush: true)

        then:
        instance.hasErrors()
        invalidFields.collect { field ->
            instance.errors.getFieldErrorCount(field)
        }.sum() == instance.errors.errorCount

        where:
        code << ["LN1234567890424567"]
        authCode << ["80283234023420234362"]
        management << [Terminal.Management.Recorded]
        lastAccessAddress << [InetAddress.getByName("www.163.com")]
        invalidFields << [['modelCode', 'groupName', 'management']]
    }
}
